ShowTable of Contents
Overview
The various options available when contributing toolbars and menus can be confusing when developing Expeditor applications. To simplify the process of contributing these items, the Expeditor user interface team recommends the following best practices. Sample code has been provided below to demonstrate the following decorations to the user interface:
Toolbar Action | A rich SWT control added using a controlSet |
Toggle Action Set Action | An action contributed using an actionSet |
View ToolBar Action | An action added to a ViewPart's toolbar |
View Menu Action | An action added to a ViewPart's menu |
Global Toolbar
- Use org.eclipse.ui.actionSets, if the developer wants to add regular/standard type of toolbar button such as push, radio, toggle, pulldown buttons
- Use com.ibm.rcp.ui.controlSets, if the developer wants to add arbitrary SWT control to toolbar, such as Text, Combo, StyledText, Browser controls
- The extension point org.eclipse.ui.menus is not supported for the global toolbar in Expeditor
Toolbar Action
The "Toolbar Action" is added using the controlSet extension point detailed in the Expeditor wiki article
Contributing custom controls to the main toolbar statically
. To add the "Toolbar Action", declare a controlSet, contribute a toolbar, and finally add the control to the toolbar. Valid entries for the path are: BEGIN_GROUP, MIDDLE_GROUP, END_GROUP.
<extension
point="com.ibm.rcp.ui.controlSets">
<controlSet
align="left"
id="com.ibm.rcp.support.menu.controlset"
label="Toolbar Samples"
showInToolBarMenu="true"
tooltip="Displays toolbar contributions"
visible="true">
<toolBar
id="com.ibm.rcp.support.menu.globaltoolbar"
path="BEGIN_GROUP">
</toolBar>
<control
class="com.ibm.rcp.support.menu.globaltoolbar.ToolbarAction"
id="com.ibm.rcp.support.menu.globaltoolbar.control"
toolbarPath="com.ibm.rcp.support.menu.globaltoolbar">
</control>
</controlSet>
</extension>
The "Toolbar Action" class snippet is below.
public class ToolbarAction extends ContributionItem implements
ISContributionItem {
private SToolItem item;
private final Image icon = Activator.imageDescriptorFromPlugin(
Activator.PLUGIN_ID, "icons" + File.separator + "notes_icon.gif")
.createImage();
public void fill(final SToolBar bar, int index) {
item = new SToolItem(bar, SWT.PUSH);
item.setText("Toolbar Action");
item.setToolTipText("A toolbar contribution");
item.setImage(icon);
item.addSelectionListener(new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent event) {
// no default
}
@Override
public void widgetSelected(SelectionEvent event) {
Item i = ((Item) event.getSource());
ActionContributionItem actionContrib = (ActionContributionItem) i
.getData();
doSomething(item, actionContrib);
}
});
}
@Override
public void fill(SCoolBar parent, int index) {
}
public void dispose() {
icon.dispose();
}
...
Toggle ActionSet Action
The "Toggle ActionSet Action" is provided as an advanced sample of the
Contributing Eclipse-based controls to the toolbar statically
wiki article. Depending on the context, some actions may need to be hidden. For example, a composite application may need to show one action while another may need to hide an action and show another. This showing and hiding is accomplished using Eclipse activities. Toolbar actions are associated with an activity, and when required to show, the corresponding activity is enabled.
The following extensions create the "Toggle ActionSet Action" as well as the "ActionSet Action". Initially the "Toggle ActionSet Action" is the only action visible.
<extension
point="org.eclipse.ui.actionSets">
<actionSet
id="com.ibm.rcp.support.menu.actionset"
label="ActionSet Action"
visible="true">
<action
class="com.ibm.rcp.support.menu.ActionSetAction"
id="com.ibm.rcp.support.menu.actionset.action"
label="ActionSet Action"
style="push"
toolbarPath="END_GROUP">
</action>
<action
class="com.ibm.rcp.support.menu.ActionSetToggle"
id="com.ibm.rcp.support.menu.actionset.action.toggle"
label="Toggle ActionSet Action"
style="push"
toolbarPath="END_GROUP">
</action>
</actionSet>
</extension>
Both classes simply implement the org.eclipse.ui.IWorkbenchWindowActionDelegate interface. The "ActionSet Action" is added to an Eclipse activity.
<extension
point="org.eclipse.ui.activities">
<activity
id="com.ibm.rcp.support.menu.activity"
name="Action Set" />
<activityPatternBinding
activityId="com.ibm.rcp.support.menu.activity"
isEqualityPattern="false"
pattern="com\.ibm\.rcp\.support\.menu/com\.ibm\.rcp\.support\.menu\.actionset\.action" />
</extension>
The pattern is a regular expression; it declares pluginID/actionID. By enabling the activity com.ibm.rcp.support.menu.activity, the Eclipse action defined by the ID com.ibm.rcp.support.menu.actionset.action in the plugin com.ibm.rcp.support.menu is shown. And this is exactly what the "Toggle ActionSet Action" button does, it enables the activity which shows the "ActionSet Action".
// the activity ID from the plugin.xml
// this binds the actionSet contribution to the activity
String id = "com.ibm.rcp.support.menu.activity";
IWorkbenchActivitySupport s = PlatformUI.getWorkbench()
.getActivitySupport();
IActivityManager m = s.getActivityManager();
IActivity a = m.getActivity(id);
// toggle
if (a != null) {
Set enabled;
if (a.isEnabled()) {
enabled = new HashSet(m.getEnabledActivityIds());
enabled.remove(id);
} else {
enabled = new HashSet(m.getEnabledActivityIds());
enabled.add(id);
}
s.setEnabledActivityIds(enabled);
}
Composite Applications and Activities
Expeditor Composite Applications can enable and disable activities. For example when a Composite Application is shown, the activity is enabled, and when the Composite Application is closed or deactivated, the activity is disabled. This gives the show-hide context described earlier. To bind a particular Composite Application to an activity, edit the Composite Application's page properties - activities are defined at the page level not the application level. Add the property com.ibm.rcp.activities=activityId. Multiple activities can be delimited by the token colon colon (::).
View Toolbars
- Use org.eclipse.ui.viewActions, if the developer wants to add regular/standard type of toolbar button such as push, radio, toggle, pulldown buttons.
- Call IViewSite.getActionBars().getToolbarManager() and add a contribution to the toolbar manager , if the developer wants to add arbitrary SWT control to toolbar, such as Text, Combo, StyledText, Browser controls.
- Extension point org.eclipse.ui.menus is not supported for a view's toolbar in Expeditor.
View ToolBar Action and View Menu Action
The "View ToolBar Action" and "View Menu Action" are added programmatically using method two described above. The code for doing this is relatively straightforward and has been provided as part of a ViewPart's creation method.
public void createPartControl(Composite parent) {
// get menu managers
org.eclipse.jface.action.IToolBarManager toolBar = super.getViewSite()
.getActionBars().getToolBarManager();
org.eclipse.jface.action.IMenuManager menuBar = getViewSite()
.getActionBars().getMenuManager();
// create actions
org.eclipse.jface.action.Action toolAction = new Action() {
public void run() {
// do something
}
};
toolAction.setText("View ToolBar Action");
org.eclipse.jface.action.Action menuAction = new Action() {
public void run() {
// do something
}
};
menuAction.setText("View Menu Action");
// add actions to menu managers
toolBar.add(toolAction);
menuBar.add(menuAction);
}